<?php

namespace app\common\business\news;

use app\common\business\BaseBus;

class SubscripBus extends BaseBus
{
    protected $model;

    public function __construct()
    {
        $this->model = new \app\common\model\Subscription();
    }

    /**
     * 订阅
     * @param $email
     * @return bool
     * @throws \support\exception\BusinessException
     */
    public function subscrip($email='')
    {
        if (empty($email)) {
            return self::errorBus(trans('Please input your email'));
        };
        $hasSubscrip = $this->getFindData(['email' => $email], 'id');
        try {
            if (!$hasSubscrip) {
                $this->model::create([
                    'email' => $email
                ]);
            } else {
                $hasSubscrip->updated_at = time();
                $hasSubscrip->save();
            }
        } catch (\Exception $e) {
            return self::errorBus(trans('Subscription failed') . ' #1');
        }
        return true;
    }
}
